/*     */ package com.zimbra.cs.redolog.op;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.MailboxOperation;
/*     */ import com.zimbra.cs.redolog.RedoLogInput;
/*     */ import com.zimbra.cs.redolog.RedoLogOutput;
/*     */ import com.zimbra.cs.redolog.Version;
/*     */ import java.io.IOException;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ImapCopyItem
/*     */   extends RedoableOp
/*     */ {
/*  33 */   private Map<Integer, Integer> mDestIds = new HashMap();
/*  34 */   private Map<Integer, String> mDestUuids = new HashMap();
/*     */   private MailItem.Type type;
/*     */   private int mDestFolderId;
/*     */   
/*     */   public ImapCopyItem() {
/*  39 */     super(MailboxOperation.ImapCopyItem);
/*  40 */     this.type = MailItem.Type.UNKNOWN;
/*  41 */     this.mDestFolderId = 0;
/*     */   }
/*     */   
/*     */   public ImapCopyItem(int mailboxId, MailItem.Type type, int folderId) {
/*  45 */     this();
/*  46 */     setMailboxId(mailboxId);
/*  47 */     this.type = type;
/*  48 */     this.mDestFolderId = folderId;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void setDest(int srcId, int destId, String destUuid)
/*     */   {
/*  56 */     this.mDestIds.put(Integer.valueOf(srcId), Integer.valueOf(destId));
/*  57 */     this.mDestUuids.put(Integer.valueOf(srcId), destUuid);
/*     */   }
/*     */   
/*     */   public int getDestId(int srcId) {
/*  61 */     Integer destId = (Integer)this.mDestIds.get(Integer.valueOf(srcId));
/*  62 */     return destId == null ? -1 : destId.intValue();
/*     */   }
/*     */   
/*     */   public String getDestUuid(int srcId) {
/*  66 */     return (String)this.mDestUuids.get(Integer.valueOf(srcId));
/*     */   }
/*     */   
/*     */   protected String getPrintableData()
/*     */   {
/*  71 */     StringBuilder sb = new StringBuilder("type=").append(this.type);
/*  72 */     sb.append(", destFolder=").append(this.mDestFolderId);
/*  73 */     sb.append(", [srcId, destId, destUuid]=");
/*  74 */     for (Map.Entry<Integer, Integer> entry : this.mDestIds.entrySet()) {
/*  75 */       int srcId = ((Integer)entry.getKey()).intValue();
/*  76 */       sb.append('[').append(srcId).append(',').append(entry.getValue());
/*  77 */       sb.append(',').append((String)this.mDestUuids.get(Integer.valueOf(srcId))).append(']');
/*     */     }
/*  79 */     return sb.toString();
/*     */   }
/*     */   
/*     */   protected void serializeData(RedoLogOutput out) throws IOException
/*     */   {
/*  84 */     out.writeByte(this.type.toByte());
/*  85 */     out.writeInt(this.mDestFolderId);
/*  86 */     out.writeShort((short)-1);
/*  87 */     out.writeInt(this.mDestIds.size());
/*  88 */     for (Map.Entry<Integer, Integer> entry : this.mDestIds.entrySet()) {
/*  89 */       Integer srcId = (Integer)entry.getKey();
/*  90 */       out.writeInt(srcId.intValue());
/*  91 */       out.writeInt(((Integer)entry.getValue()).intValue());
/*  92 */       out.writeInt(-1);
/*  93 */       if (getVersion().atLeast(1, 37)) {
/*  94 */         out.writeUTF((String)this.mDestUuids.get(srcId));
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   protected void deserializeData(RedoLogInput in) throws IOException
/*     */   {
/* 101 */     this.type = MailItem.Type.of(in.readByte());
/* 102 */     this.mDestFolderId = in.readInt();
/* 103 */     in.readShort();
/* 104 */     int count = in.readInt();
/* 105 */     for (int i = 0; i < count; i++) {
/* 106 */       Integer srcId = Integer.valueOf(in.readInt());
/* 107 */       this.mDestIds.put(srcId, Integer.valueOf(in.readInt()));
/* 108 */       in.readInt();
/* 109 */       if (getVersion().atLeast(1, 37)) {
/* 110 */         this.mDestUuids.put(srcId, in.readUTF());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void redo() throws Exception
/*     */   {
/* 117 */     int mboxId = getMailboxId();
/* 118 */     Mailbox mbox = MailboxManager.getInstance().getMailboxById(mboxId);
/*     */     
/* 120 */     int i = 0;int[] itemIds = new int[this.mDestIds.size()];
/* 121 */     for (Iterator i$ = this.mDestIds.keySet().iterator(); i$.hasNext();) { int id = ((Integer)i$.next()).intValue();
/* 122 */       itemIds[(i++)] = id;
/*     */     }
/*     */     try {
/* 125 */       mbox.imapCopy(getOperationContext(), itemIds, this.type, this.mDestFolderId);
/*     */     } catch (MailServiceException e) {
/* 127 */       if (e.getCode() == "mail.ALREADY_EXISTS") {
/* 128 */         mLog.info("Item is already in mailbox " + mboxId);
/* 129 */         return;
/*     */       }
/* 131 */       throw e;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/redolog/op/ImapCopyItem.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */